﻿<cfsilent>
	<cfscript>

		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		imgObj = getProperty("serviceFactory").getBean("imageObject");
		fileObj = getProperty("serviceFactory").getBean("tagFileHelper");
		contentObj = getProperty("serviceFactory").getBean("tagContentHelper");

		/* 学籍相关信息 */
		userId = sessionAdvice.getAutherUserID();
		userGroups = sessionAdvice.getAuthorizedUserGroups();
			
		/*
			P001	教师
			P002	学生
		*/
		if ( listFind( userGroups, "P001", "," ) ) {
			userType = "Teacher";
		}
		if ( listFind( userGroups, "P002", "," ) ) {
			userType = "Student";
		}

		tchId = event.getArg("TCH");
		type = event.getArg("STR");
		
		/* 尝试自动更新教师照片信息 */
		uploadFile = getProperty("cdnAssetsPath") & "/tmp/tchPicture/" & tchId & ".jpg";
		
		if ( fileExists(uploadFile) ) {
			
			portraitDir = getProperty("cdnAssetsPath") & "/teacher/portrait/";
			profileDir = getProperty("cdnAssetsPath") & "/teacher/profile/";
			printDir = getProperty("cdnAssetsPath") & "/teacher/print/";
			
			if ( not directoryExists( portraitDir ) ) {
				directoryCreate( portraitDir );
			}
			if ( not directoryExists( profileDir ) ) {
				directoryCreate( profileDir );
			}
			if ( not directoryExists( printDir ) ) {
				directoryCreate( printDir );
			}
			
			/* 如果有上传的待处理照片 则自动生成各规格图片 */

			/* 小头像 50x50 */
			if ( fileExists( portraitDir & tchId & ".jpg" ) ) {
				fileObj.delete( portraitDir & tchId & ".jpg" );
			}
			
			imgObj.readImage( uploadFile );
				
			imgObj.scaleWidth( 152 );
			imgObj.blur( 1 );
			imgObj.crop( 1, max( (imgObj.getHeight()-150)/2 - 15, 1 ), 150, 150 );
			imgObj.scaleWidth( 50 );
			imgObj.writeImage( portraitDir & tchId & ".jpg", "jpg", 100);
			
			/* 将图片对象放置到系统缓存 */
			binaryStream = fileObj.readBinary( portraitDir & tchId & ".jpg" );
			cachePut( "user_portrait_" & tchId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
			

			/* 证件照 135x180 */
			if ( fileExists( profileDir & tchId & ".jpg" ) ) {
				fileObj.delete( profileDir & tchId & ".jpg" );
			}

			imgObj.readImage( uploadFile );

			imgObj.scaleWidth( 405 );
			imgObj.blur( 1 );
			imgObj.scaleWidth( 137 );
			imgObj.crop( 1, (imgObj.getHeight()-180)/2, 135, 180 );
			imgObj.writeImage( profileDir & tchId & ".jpg", "jpg", 100);
			
			/* 将图片对象放置到系统缓存 */
			binaryStream = fileObj.readBinary( profileDir & tchId & ".jpg" );
			cachePut( "user_profile_" & tchId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
			
			
			/* 打印照片 480x640 */
			if ( fileExists( printDir & tchId & ".jpg" ) ) {
				fileObj.delete( printDir & tchId & ".jpg" );
			}

			imgObj.readImage( uploadFile );

			imgObj.scaleWidth( 484 );
			imgObj.crop( 2, (imgObj.getHeight()-640)/2, 480, 640 );
			imgObj.writeImage( printDir & tchId & ".jpg", "jpg", 100);
			
			/* 将图片对象放置到系统缓存 */
			binaryStream = fileObj.readBinary( printDir & tchId & ".jpg" );
			cachePut( "user_print_" & tchId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
			
			
			/* 删除临时文件 */
			fileDelete( uploadFile );
			
		}
		
		switch (type) {
			
			case "portrait":
				
				/* 50x50 */
				target = getProperty("cdnAssetsPath") & "/profile/default_s.jpg";
				
				binaryStream = cacheGet( "user_portrait_default", "matrixPicture" );
				
				if ( isNull(binaryStream) ) {
					binaryStream = fileObj.readBinary( target );
					/* 将默认文件对象放置到系统缓存 */
					cachePut( "user_portrait_default", binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
				}
				
				if ( userType eq "Teacher") {
					
					binaryStream = cacheGet( "user_portrait_" & tchId, "matrixPicture" );
					
					if ( isNull(binaryStream) ) {
					
						newTarget = getProperty("cdnAssetsPath") & "/teacher/portrait/" & tchId & ".jpg";
					
						if ( fileExists(newTarget) ) {
							target = newTarget;
						}
						
						binaryStream = fileObj.readBinary( target );
						/* 将图片对象放置到系统缓存 */
						cachePut( "user_portrait_" & tchId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
					}
					
				}
				
				
				break;
			
			case "profile":
				
				/* 180x135 */
				target = getProperty("cdnAssetsPath") & "/profile/default.jpg";
				
				binaryStream = cacheGet( "user_profile_default", "matrixPicture" );
				
				if ( isNull(binaryStream) ) {
					binaryStream = fileObj.readBinary( target );
					/* 将默认文件对象放置到系统缓存 */
					cachePut( "user_profile_default", binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
				}
				
				if ( userType eq "Teacher") {
					
					binaryStream = cacheGet( "user_profile_" & tchId, "matrixPicture" );
					
					newTarget = getProperty("cdnAssetsPath") & "/teacher/profile/" & tchId & ".jpg";
					
					if ( isNull(binaryStream) ) {
					
						if ( fileExists(newTarget) ) {
							target = newTarget;
						}
						
						binaryStream = fileObj.readBinary( target );
						/* 将图片对象放置到系统缓存 */
						cachePut( "user_profile_" & tchId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
						
					}
						
				}
				
				break;
				
			case "print":
				
				/* 480x640 */
				target = getProperty("cdnAssetsPath") & "/profile/default_p.jpg";
				
				binaryStream = cacheGet( "user_print_default", "matrixPicture" );
				
				if ( isNull(binaryStream) ) {
					binaryStream = fileObj.readBinary( target );
					/* 将默认文件对象放置到系统缓存 */
					cachePut( "user_print_default", binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
				}
				
				if ( userType eq "Teacher") {
					
					binaryStream = cacheGet( "user_print_" & tchId, "matrixPicture" );
					
					if ( isNull(binaryStream) ) {
					
						newTarget = getProperty("cdnAssetsPath") & "/teacher/print/" & tchId & ".jpg";
					
						if ( fileExists(newTarget) ) {
							target = newTarget;
						}
						
						binaryStream = fileObj.readBinary( target );
						/* 将图片对象放置到系统缓存 */
						cachePut( "user_print_" & tchId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
						
					}
					
				}
				
				break;
			
		}
		
		contentObj.contentWriteThrough( "image/jpeg", binaryStream, true );

	</cfscript>
</cfsilent>